﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

using YEngineLib;
using YEngineLib.Graphics;
using YEngineLib.Input;
using YEngineLib.State;
using YEngineLib.Util;

//using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
//using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
//using Microsoft.Xna.Framework.Graphics;
//using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using System.Threading;
using Spyfall.Objects;

using System.IO;

namespace Spyfall.States
{
    class MainMenuState : YBaseGameState
    {
        // Gamepad states used to determine button presses
        GamePadState currentGamePadState;
        GamePadState previousGamePadState;

        private Managers.ObjectManager objectManager;
        private Managers.MessageManager msgManager;
        public PlayerIndex daPlaya;
        Player PlayerObj;
        GameState reset;
        private Viewport viewPort;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        List<String> menuOptions = new List<String>();
        int selectedMenuOption = 1;
        Vector2 screenPosition;
        SpriteFont spriteFont;
        //private MainMenuBackground mmBackground;
        Texture2D skyBackground;
        Texture2D plane;
        Texture2D playSelectedPic;
        Texture2D shopSelectedPic;
        Texture2D howSelectedPic;
        Texture2D endlessSelectedPic;
        Texture2D creditsSelectedPic;
        Texture2D exitSelectedPic;
        Texture2D howToPlayPic;
        Texture2D creditsPic;
        Texture2D cloudPic;
        Texture2D[] mmGuyPic = new Texture2D[4];
        Texture2D[] FirePic = new Texture2D[4];
        Texture2D trialPic;
        Rectangle wholescreen;
        bool howToPlay;
        bool credits;
        int myFrame;
        float myFrameTimer;
        Vector2 bottomCloudPos;
        Vector2 topCloudPos;

        int highscoreTime;
        int onLevel;

        bool trialMode;
        bool showTrialScreen;

        //Sound Stuff ****************************************
        SoundEffect selectButton;
        SoundEffect selected;
        SoundEffect store;

//Buttons-------------------------------------------------------------
        //private StartButton startButton;
        //private StartButtonSelected startButtonSelected;
        //private ExitButton exitButton;
        //private ExitButtonSelected exitButtonSelected;
        //private ResumeButtonSelected resumeButtonSelected;
        //private ResumeButton resumeButton;
        //private ShopButton shopButton;
        //private ShopButtonSelected shopButtonSelected;
        //private CreditButton creditButton;
        //private CreditButtonSelected creditButtonSelected;

        public MainMenuState()
        {
            PlayerObj = new Player();
            daPlaya = (PlayerIndex)0;
            objectManager = new Managers.ObjectManager();
            msgManager = new Managers.MessageManager();
            screenPosition = new Vector2(10.0f, 10.0f);
            //mmBackground = new MainMenuBackground();
            //mmBackground.Initialize();
            reset = new GameState();
//Buttons-------------------------------------------------------------
            //startButton = new StartButton();
            //startButton.Initialize();
            //startButtonSelected = new StartButtonSelected();
            //startButtonSelected.Initialize();
            //exitButton = new ExitButton();
            //exitButtonSelected = new ExitButtonSelected();
            //exitButton.Initialize();
            //exitButtonSelected.Initialize();
            //resumeButton = new ResumeButton();
            //resumeButton.Initialize();
            //resumeButtonSelected = new ResumeButtonSelected();
            //resumeButtonSelected.Initialize();
            //shopButton = new ShopButton();
            //shopButtonSelected = new ShopButtonSelected();
            //shopButtonSelected.Initialize();
            //shopButton.Initialize();
            //creditButton = new CreditButton();
            //creditButton.Initialize();
            //creditButtonSelected = new CreditButtonSelected();
            //creditButtonSelected.Initialize();

            wholescreen = new Rectangle(0, 0, 1280, 720);
            howToPlay = false;
            credits = false;
            showTrialScreen = false;
            myFrame = 0;
            myFrameTimer = 0.0f;
            bottomCloudPos.Y = 600;
            bottomCloudPos.X = -400;
            topCloudPos.Y = 10;
            topCloudPos.X = 100;
            highscoreTime = 0;
            onLevel = 0;
        }

        public override void LoadContent()
        {
            spriteFont = YEngine.Game.Content.Load<SpriteFont>("Fonts/clock");
            howToPlayPic = YEngine.Game.Content.Load<Texture2D>("Graphics/howtoplay");
            skyBackground = YEngine.Game.Content.Load<Texture2D>("Graphics/mmsky");
            plane = YEngine.Game.Content.Load<Texture2D>("Graphics/mmplane");
            playSelectedPic = YEngine.Game.Content.Load<Texture2D>("Graphics/playselected");
            shopSelectedPic = YEngine.Game.Content.Load<Texture2D>("Graphics/shopselected");
            howSelectedPic = YEngine.Game.Content.Load<Texture2D>("Graphics/howselected");
            endlessSelectedPic = YEngine.Game.Content.Load<Texture2D>("Graphics/endlessselected");
            creditsSelectedPic = YEngine.Game.Content.Load<Texture2D>("Graphics/creditsselected");
            exitSelectedPic = YEngine.Game.Content.Load<Texture2D>("Graphics/exitselected");
            mmGuyPic[0] = YEngine.Game.Content.Load<Texture2D>("Graphics/mmguy1");
            mmGuyPic[1] = YEngine.Game.Content.Load<Texture2D>("Graphics/mmguy2");
            mmGuyPic[2] = YEngine.Game.Content.Load<Texture2D>("Graphics/mmguy3");
            mmGuyPic[3] = YEngine.Game.Content.Load<Texture2D>("Graphics/mmguy4");
            FirePic[0] = YEngine.Game.Content.Load<Texture2D>("Graphics/fire1");
            FirePic[1] = YEngine.Game.Content.Load<Texture2D>("Graphics/fire2");
            FirePic[2] = YEngine.Game.Content.Load<Texture2D>("Graphics/fire3");
            FirePic[3] = YEngine.Game.Content.Load<Texture2D>("Graphics/fire4");
            cloudPic = YEngine.Game.Content.Load<Texture2D>("Graphics/cloud01");
            creditsPic = YEngine.Game.Content.Load<Texture2D>("Graphics/creditsscreen");
            trialPic = YEngine.Game.Content.Load<Texture2D>("Graphics/trialscreen");

            //Sound Stuff ****************************************
            selectButton = YEngine.Game.Content.Load<SoundEffect>("Sounds/SFX_SELECTHIGHLIGHT");
            selected = YEngine.Game.Content.Load<SoundEffect>("Sounds/SFX_BUTTONSELECT");
            store = YEngine.Game.Content.Load<SoundEffect>("Sounds/SFX_STORE");

            LoadPercentage = 100.0f;
        }

        public override void OnEnter(string previousState)
        {
            currentGamePadState = GamePad.GetState(daPlaya);
            viewPort = YEngine.Graphics.ScreenViewport;
            menuOptions.Add("TEST");

#if XBOX
            if (Guide.IsTrialMode)
                trialMode = true;

            // get the highscore data/read the save file
            if (Global.SaveDevice.FileExists(Global.containerName, Global.fileName_Levels))
            {
                Global.SaveDevice.Load(
                    Global.containerName,
                    Global.fileName_Levels,
                    stream =>
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            highscoreTime = int.Parse(reader.ReadLine());
                            onLevel = int.Parse(reader.ReadLine());
                        }
                    });
            }
#endif

        }

        public override void OnExit(string nextState)
        {
            //get out of here!!!
            reset.Initialize();
        }

        public override void Input()
        {
            previousGamePadState = currentGamePadState;
            currentGamePadState = GamePad.GetState(daPlaya);
           // PlayerObj.velocity.X = currentGamePadState.ThumbSticks.Left.X;
           // PlayerObj.velocity.Y = -currentGamePadState.ThumbSticks.Left.Y;
//----------------------------------------------------------------------------------- WRAP  

            if (showTrialScreen)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter) || (currentGamePadState.Buttons.B == ButtonState.Pressed && previousGamePadState.Buttons.B != ButtonState.Pressed))
                {
                    showTrialScreen = false;
                }
                return;
            }

            if (howToPlay )
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter) || ( currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed))
                {
                    howToPlay = false;
                    
                }
                return;
            }

            if (credits)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter) || (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed))
                {
                    credits = false;
                    
                }
                return;
            }

            if (YEngine.Input.Keyboard.KeyPressed(Keys.Right) || (previousGamePadState.IsButtonUp(Buttons.LeftThumbstickRight) && currentGamePadState.IsButtonDown(Buttons.LeftThumbstickRight)))
            {
                if (selectedMenuOption <= 3)
                {
                    selectButton.Play(0.8f, 0.0f, 0.0f);
                    selectedMenuOption += 3;
                }
            }

            if (YEngine.Input.Keyboard.KeyPressed(Keys.Left) || (previousGamePadState.IsButtonUp(Buttons.LeftThumbstickLeft) && currentGamePadState.IsButtonDown(Buttons.LeftThumbstickLeft)))
            {
                if (selectedMenuOption > 3)
                {
                    selectButton.Play(0.8f, 0.0f, 0.0f);
                    selectedMenuOption -= 3;
                }
            }

            if (YEngine.Input.Keyboard.KeyPressed(Keys.Down) || (previousGamePadState.IsButtonUp(Buttons.LeftThumbstickDown) && currentGamePadState.IsButtonDown(Buttons.LeftThumbstickDown)))
            {
                if (selectedMenuOption == 3)    //theres a Wrap function.. check that out later.
                {
                    selectedMenuOption = 0;
                }
                else if (selectedMenuOption == 6)
                {
                    selectedMenuOption = 3;
                }
                selectButton.Play(0.8f, 0.0f, 0.0f);
                selectedMenuOption += 1;
            }
            if (YEngine.Input.Keyboard.KeyPressed(Keys.Up) || (previousGamePadState.IsButtonUp(Buttons.LeftThumbstickUp) && currentGamePadState.IsButtonDown(Buttons.LeftThumbstickUp)))
            {
                if (selectedMenuOption == 1)
                {
                    selectedMenuOption = 4;
                }
                else if (selectedMenuOption == 4)
                {
                    selectedMenuOption = 7;
                }
                selectButton.Play(0.8f, 0.0f, 0.0f);
                selectedMenuOption -= 1;
            }

           // if ((previousGamePadState.IsButtonUp(Buttons.LeftThumbstickDown) && currentGamePadState.IsButtonDown(Buttons.LeftThumbstickDown)))
           // {
           //     if (selectedMenuOption == 5)
           //     {
           //         selectedMenuOption = 0;
           //     }
           //     selectedMenuOption += 1;
           // }
           //
           // if ((previousGamePadState.IsButtonUp(Buttons.LeftThumbstickUp) && currentGamePadState.IsButtonDown(Buttons.LeftThumbstickUp)))
           // {
           //     if (selectedMenuOption == 1)
           //     {
           //         selectedMenuOption = 6;
           //     }
           //     selectedMenuOption -= 1;
           // }
//----------------------------------------------------------------------------------- WRAP 
            if (YEngine.Input.Keyboard.KeyPressed(Keys.Escape))
                YEngine.Game.Exit();

            if (selectedMenuOption == 1)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    //howToPlay = true;
                    //YEngine.State.ChangeState("HowToPlayState");
                    //return;
                    YEngine.State.ChangeState("GameState");
                    selected.Play(0.8f, 0.0f, 0.0f);
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed)
                {
                    //howToPlay = true;
                    //YEngine.State.ChangeState("HowToPlayState");
                    //return;
                    YEngine.State.ChangeState("GameState");
                    selected.Play(0.8f, 0.0f, 0.0f);
                    return;
                }
            }
            if (selectedMenuOption == 2)
            {

                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    if (trialMode)
                    {
                        showTrialScreen = true;
                        selected.Play(0.8f, 0.0f, 0.0f);
                        return;
                    }
                    YEngine.State.ChangeState("ShopState");
                    store.Play(0.8f, 0.0f, 0.0f);
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed)
                {
                    if (trialMode)
                    {
                        showTrialScreen = true;
                        store.Play(0.8f, 0.0f, 0.0f);
                        return;
                    }
                    YEngine.State.ChangeState("ShopState");
                    store.Play(0.8f, 0.0f, 0.0f);
                    return;
                }
            }
            if (selectedMenuOption == 3)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    credits = true;
                    selected.Play(0.8f, 0.0f, 0.0f);
                    //YEngine.State.ChangeState("CreditsState");
                    //return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed)
                {
                    credits = true;
                    selected.Play(0.8f, 0.0f, 0.0f);
                    //YEngine.State.ChangeState("CreditsState");
                    //return;
                }
            }
            if (selectedMenuOption == 4)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    if (trialMode)
                    {
                        showTrialScreen = true;
                        selected.Play(0.8f, 0.0f, 0.0f);
                        return;
                    }
                    // endless here
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed)
                {
                    if (trialMode)
                    {
                        showTrialScreen = true;
                        selected.Play(0.8f, 0.0f, 0.0f);
                        return;
                    }
                    //YEngine.State.ChangeState("CreditsState");
                    //return;
                }
            }
            if (selectedMenuOption == 5)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    //YEngine.Game.Exit();
                    howToPlay = true;
                    selected.Play(0.8f, 0.0f, 0.0f);
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed)
                {
                    //YEngine.Game.Exit();
                    howToPlay = true;
                    selected.Play(0.8f, 0.0f, 0.0f);
                    return;
                }
            }

            if (selectedMenuOption == 6)
            {
                if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter))
                {
                    YEngine.Game.Exit();
                    selected.Play(0.8f, 0.0f, 0.0f);
                    return;
                }
                if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed)
                {
                    YEngine.Game.Exit();
                    selected.Play(0.8f, 0.0f, 0.0f);
                    return;
                }
            }
        }

        public override void Update() 
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                YEngine.Game.Exit();
            objectManager.UpdateObjects((float)YEngine.Time.GameTime.ElapsedGameTime.TotalSeconds);

            myFrameTimer += (float)YEngine.Time.GameTime.ElapsedGameTime.TotalSeconds;
            //myFrameTimer += 0.1f;

            bottomCloudPos.X += 40 * (float)YEngine.Time.GameTime.ElapsedGameTime.TotalSeconds;
            topCloudPos.X += 30 * (float)YEngine.Time.GameTime.ElapsedGameTime.TotalSeconds;

            if (topCloudPos.X > 1300)
                topCloudPos.X = -400;

            if (bottomCloudPos.X > 1300)
                bottomCloudPos.X = -400;

            if (myFrameTimer >= 0.1f)
            {
                myFrameTimer = 0.0f;
                myFrame++;
                if (myFrame >= 4)
                {
                    myFrame = 0;
                }
            }
        }

        public override void Draw()
        {
            YEngine.Graphics.SpriteBatch.Begin();
            //mmBackground.Draw();

            YEngine.Graphics.SpriteBatch.Draw(skyBackground, wholescreen, Color.White);

            YEngine.Graphics.SpriteBatch.Draw(cloudPic, bottomCloudPos, Color.White);
            YEngine.Graphics.SpriteBatch.Draw(cloudPic, topCloudPos, Color.White);

            YEngine.Graphics.SpriteBatch.Draw(plane, wholescreen, Color.White);
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "" + highscoreTime / 60, new Vector2(1095, 230), Color.Black);
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, ":", new Vector2(1112, 230), Color.Black);
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "" + highscoreTime % 60, new Vector2(1120, 230), Color.Black);
            YEngine.Graphics.SpriteBatch.Draw(FirePic[myFrame], wholescreen, Color.White);
            YEngine.Graphics.SpriteBatch.Draw(mmGuyPic[myFrame], wholescreen, Color.White);

            if (selectedMenuOption == 1)
            {
                YEngine.Graphics.SpriteBatch.Draw(playSelectedPic, wholescreen, Color.White);
            }
            else if (selectedMenuOption == 2)
            {
                YEngine.Graphics.SpriteBatch.Draw(shopSelectedPic, wholescreen, Color.White);
            }

            else if (selectedMenuOption == 3)
            {
                YEngine.Graphics.SpriteBatch.Draw(creditsSelectedPic, wholescreen, Color.White);
            }

            else if (selectedMenuOption == 4)
            {
                YEngine.Graphics.SpriteBatch.Draw(endlessSelectedPic, wholescreen, Color.White);
            }

            else if (selectedMenuOption == 5)
            {
                YEngine.Graphics.SpriteBatch.Draw(howSelectedPic, wholescreen, Color.White);
            }

            else if (selectedMenuOption == 6)
            {
                YEngine.Graphics.SpriteBatch.Draw(exitSelectedPic, wholescreen, Color.White);
            }

            if (showTrialScreen)
                YEngine.Graphics.SpriteBatch.Draw(trialPic, wholescreen, Color.White);

            if(howToPlay)
                YEngine.Graphics.SpriteBatch.Draw(howToPlayPic, wholescreen, Color.White);

            if (credits)
                YEngine.Graphics.SpriteBatch.Draw(creditsPic, wholescreen, Color.White);

            Vector2 menuTopCenter = new Vector2(400, 300);
            float fontHeight = spriteFont.MeasureString("ANY").Y;

            YEngine.Graphics.SpriteBatch.End();
        }
    }
}
